Conditions | 19 |
Total Lines | 95 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like custom.js ➔ AJAX often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | |||
110 | function AJAX(url, method, params, options) { |
||
111 | var xhr = null; |
||
112 | |||
113 | try { // For: chrome, firefox, safari, opera, yandex, ... |
||
114 | xhr = new XMLHttpRequest(); |
||
115 | } catch(e) { |
||
116 | try { // For: IE6+ |
||
117 | xhr = new ActiveXObject("Microsoft.XMLHTTP"); |
||
118 | } catch(e1) { // if JS not supported or disabled |
||
119 | console.log("Browser Not supported!"); |
||
120 | return; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | xhr.onreadystatechange = function() { |
||
125 | |||
126 | // ready states: |
||
127 | // 0: uninitialized |
||
128 | // 1: loading |
||
129 | // 2: loaded |
||
130 | // 3: interactive |
||
131 | // 4: complete |
||
132 | |||
133 | if (xhr.readyState == 4) { // when result is ready |
||
134 | |||
135 | var response_text = xhr.responseText; |
||
136 | |||
137 | if ('response_json' in options && options.response_json) { |
||
138 | try { |
||
139 | response_text = JSON.parse(response_text); |
||
140 | } catch (e) { } |
||
141 | } |
||
142 | |||
143 | if (xhr.status === 200) { // on success |
||
144 | if ('func_callback' in options && typeof options.func_callback == 'function') { |
||
145 | var fc = options.func_callback; |
||
146 | fc(response_text); |
||
147 | } |
||
148 | } else { // on error |
||
149 | console.log(xhr.status + ': ' + xhr.statusText); |
||
150 | if ('func_error' in options && typeof options.func_error == 'function') { |
||
151 | var fe = options.func_error; |
||
152 | fe(response_text, xhr); |
||
153 | } |
||
154 | } |
||
155 | } else { // waiting for result |
||
156 | if ('func_waiting' in options && typeof options.func_waiting == 'function') { |
||
157 | var fw = options.func_waiting; |
||
158 | fw(); |
||
159 | } |
||
160 | } |
||
161 | }; |
||
162 | |||
163 | var data = null; |
||
164 | |||
165 | if (params.files) { |
||
166 | method = 'POST'; |
||
167 | |||
168 | data = new FormData(); |
||
169 | for (var index_param in params) { |
||
170 | if (typeof params[index_param] == 'object') { |
||
171 | for (var index_file in params[index_param]) { |
||
172 | data.append(index_file, params[index_param][index_file]); |
||
173 | } |
||
174 | } else { |
||
175 | data.append(index_param, params[index_param]); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | if ('func_progress' in options && typeof options.func_progress == 'function') { |
||
180 | xhr.upload.onprogress = function(event) { |
||
181 | // 'progress: ' + event.loaded + ' / ' + event.total; |
||
182 | var fp = options.func_progress; |
||
183 | fp(event); |
||
184 | } |
||
185 | } |
||
186 | } else { |
||
187 | data = serializeParams(params); |
||
188 | } |
||
189 | |||
190 | method = method.toUpperCase(); |
||
191 | |||
192 | if (method == 'GET' && data) { |
||
193 | url += '?' + data; |
||
194 | } |
||
195 | |||
196 | xhr.open(method, url, true); |
||
197 | |||
198 | if ( ! params.files) { |
||
199 | xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); |
||
200 | } |
||
201 | |||
202 | xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); |
||
203 | xhr.send(data); |
||
204 | } |
||
205 | |||
348 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.